package cn.cuiqi.dynamic.thread.pool.sdk.registry.redis;

import cn.cuiqi.dynamic.thread.pool.sdk.domain.model.Entity.ThreadPoolConfigEntity;
import cn.cuiqi.dynamic.thread.pool.sdk.domain.model.valObj.RegistryEnumVo;
import cn.cuiqi.dynamic.thread.pool.sdk.registry.IRegistry;
import com.alibaba.fastjson.JSON;
//import jdk.nashorn.internal.objects.annotations.Getter;
import org.redisson.api.RBucket;
import org.redisson.api.RList;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.time.Duration;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author: Cuiqi
 * @Description: Redis注册中心
 * @Date: 2024/8/1 20:47
 **/
@Component
public class RedisRegistry implements IRegistry {
    
    private final Logger logger = LoggerFactory.getLogger(RedisRegistry.class);

    private final RedissonClient redissonClient;

    
    @Autowired
    public RedisRegistry(RedissonClient redissonClient) {
        this.redissonClient = redissonClient;
    }

    @Override
    public void reportThreadPoolList(List<ThreadPoolConfigEntity> requestEntities) {
        RList<ThreadPoolConfigEntity> list = redissonClient.getList(RegistryEnumVo.THREAD_POOL_CONFIG_LIST_KEY.getKey());
        logger.info("从redis获取线程池信息：{}", JSON.toJSONString(list));
        //排除已有
        if (null != list && !list.isEmpty()) {
//            Map<String, ThreadPoolConfigEntity> allMap = list.stream().collect(Collectors.toMap(ThreadPoolConfigEntity::getThreadPoolName, Function.identity()));
//            List<ThreadPoolConfigEntity> noContains = requestEntities.stream().filter(threadPoolConfigEntity -> !allMap.containsKey(threadPoolConfigEntity.getThreadPoolName())).collect(Collectors.toList());
//            logger.info("redis中不存在的：{}", JSON.toJSONString(noContains));
//            
//            List<ThreadPoolConfigEntity> defferenceList = requestEntities.stream().filter(threadPoolConfigEntity -> allMap.containsKey(threadPoolConfigEntity.getThreadPoolName()) &&
//                    (threadPoolConfigEntity.getCorePoolSize() == allMap.get(threadPoolConfigEntity.getThreadPoolName()).getCorePoolSize() ||
//                            threadPoolConfigEntity.getMaximumPoolSize() == allMap.get(threadPoolConfigEntity.getThreadPoolName()).getMaximumPoolSize()
//                    )).collect(Collectors.toList());
//            logger.info("redis中配置不同的：{}", JSON.toJSONString(defferenceList));
//            Set<String> set = defferenceList.stream().map(ThreadPoolConfigEntity::getThreadPoolName).collect(Collectors.toSet());
//            
//            list.removeIf(item -> set.contains(item.getThreadPoolName()));
//
//            list.addAll(noContains);
//            list.addAll(defferenceList);
            list.delete();
            
        } 
        
        list.addAll(requestEntities);
        
    }

    

    @Override
    public void reportThreadPoolParamter(ThreadPoolConfigEntity threadPoolConfigEntity) {
        String key = RegistryEnumVo.THREAD_POOL_CONFIG_PARAMETER_LIST_KEY.getKey() + "_" + threadPoolConfigEntity.getAppName() + "_" + threadPoolConfigEntity.getThreadPoolName();
        RBucket<ThreadPoolConfigEntity> bucket = redissonClient.getBucket(key);
        logger.info("从redis获取线程池配置信息：{}", JSON.toJSONString(bucket.get()));
        bucket.set(threadPoolConfigEntity, Duration.ofDays(30));
    }
    
    
    
}
